#include "server.h"

void * server_side ()           // on lance un serveur
{       
        registerrpc(/* prognum */ PROGNUM,
                        /* versnum */ VERSNUM,
                        /* procnum */ PROCNUM,
                        /* pointer on function */ serveur,
                        /* argument decoding */ (xdrproc_t)xdr_message,
                        /* function result encoding */ (xdrproc_t)xdr_int);

        svc_run();              /* server ready for clients */
        
        pthread_exit(0);
}


int * serveur (Message * msg)
{
								static int ret;                         // returned at the end of the 'serveur'

								int i;
                if(msg->type != SEARCH && msg->type != BACKSEARCH && searchContactIndex(msg->nameSender) == -1) //On ne connait pas l'émetteur, on le rajoute
                {
                        printf("\n\n* %s contacted you and had been add to your book.\n\n> ",msg->nameSender);
                        fflush(stdout);    
                        addContact(msg->nameSender,msg->ipSender);
                }
                
                
                if(msg->type==CONNECTION)               // Message d'arrivée sur le "serveur" 
                {
                        printf("\n\n* %s joined.\n\n> ",msg->nameSender);
                        fflush(stdout);    
                
                        connectionContact(msg->nameSender);
                        ret = METOO;
                }
                else
                if(msg->type==DECONNECTION)     // Message de sortie du "serveur"
                {
                        printf("\n\n* %s left.\n\n> ",msg->nameSender);
                        fflush(stdout);    
                
                        deconnectionContact(msg->nameSender);

                        ret = STANDARD; 
                }
                else
                if(msg->type==BACKSEARCH)               // Message réponse à un envoi vers inconnu pour établir le pont
                {       
                        printf("\n\n* %s had received your message and had been add to your book.\n\n> ",msg->nameSender);
                        fflush(stdout);    
                        
                        addContact(msg->nameSender,msg->ipSender);
                                                
                        ret = STANDARD;
                }
                else
                if(msg->type==STANDARD)         // Message standard
                {               
                        printf("\n# %s@%s : %s\n> ", msg->nameSender,msg->nameReceiver,msg->message);
                        fflush(stdout);

                        ret = STANDARD;
                }
                else
                if(msg->type==SEARCH)           //message dont on ne connait pas l'ip du destinataire
                {
                        if(isAlreadyReceived(msg)==FALSE)
                        {
																addMsgReceived(*msg);        
                                if(strcmp(msg->nameReceiver, adressBook.me.name)==0)
                                {
                                				if(searchContactIndex(msg->nameSender) == -1) //si on le connait pas (cas normal)
                                				{
						                                printf("\n\n* %s contacted you and had been add to your book.\n\n> ",msg->nameSender);
                       											fflush(stdout);    
						                        				addContact(msg->nameSender, msg->ipSender);
                                				}
                                
                                        printf("\n# %s@%s : %s\n> ",msg->nameSender,msg->nameReceiver,msg->message);
                                        fflush(stdout);    
                        
                                        Message *backsearch_msg = newMessage();
                                
                                        /* Message type */
                                        backsearch_msg->type = BACKSEARCH;
                                        /* Sender IP */
                                        strcpy(backsearch_msg->ipSender, adressBook.me.ip);
                                        /* Sender name */
                                        strcpy(backsearch_msg->nameSender, adressBook.me.name);
                                        /* Message contents (none) */
                                        backsearch_msg->size = 0;
                                        backsearch_msg->message = NULL;
                                        // Contact name
                                        strcpy(backsearch_msg->nameReceiver, msg->nameSender);   
                                        // Contact ip
                                        strcpy(backsearch_msg->ipReceiver, msg->ipSender);
                                        
                                        send_message(backsearch_msg);
                                }
                                else
                                {
                                        int exist = searchContactIndex(msg->nameReceiver);
                                        Message * transit;
                                        									
                                        if(exist!=-1)                           // the contact is in "adressBook"
                                        {
                                        		transit = newMessage();
															              transit->timestamp = msg->timestamp;
															              transit->type = msg->type;
															              strcpy(transit->ipSender, msg->ipSender);
															              strcpy(transit->nameSender, msg->nameSender);
															              strcpy(transit->nameReceiver, msg->nameReceiver);
															              transit->size = msg->size;
																						transit->message = malloc(RAWSIZE * sizeof(char));
																						memset(transit->message, '\0', RAWSIZE);
																						strcpy(transit->message,msg->message);
                                            strcpy(transit->ipReceiver, adressBook.contacts[exist].ip);
                                            send_message(transit);
                                        }
                                        else
                                        {
                                                for(i=0;i<adressBook.nbContacts;i++)
                                                {
                                                		transit = newMessage();
								                       							transit->timestamp = msg->timestamp;
																						        transit->type = msg->type;
																						        strcpy(transit->ipSender, msg->ipSender);
																						        strcpy(transit->nameSender, msg->nameSender);
																						        strcpy(transit->nameReceiver, msg->nameReceiver);
																						        transit->size = msg->size;
																										transit->message = malloc(RAWSIZE * sizeof(char));
																										memset(transit->message, '\0', RAWSIZE);
																										strcpy(transit->message,msg->message);
										                                                
                                                    Contact c = adressBook.contacts[i];
                    
                                                    if(c.connected)
                                                    {               
                                                            // Contact ip
                                                            strcpy(transit->ipReceiver, c.ip);
            
                                                            send_message(transit);
                                                            
                                                    }
                                                }                                                
												//free(msg);
                                        }
                                }
                                ret = STANDARD;
                        }
                        else                            // the message has been  already received
                        {
                                ret = ALREADY;
                                
                                
                        }
                }
        
        return &ret;
}

